package com.ruoyi.kubernetes.service.impl;

import com.ruoyi.kubernetes.models.CSRRequestData;
import com.ruoyi.kubernetes.models.CSRResponseData;
import com.ruoyi.kubernetes.models.ClientCSRData;
import com.ruoyi.kubernetes.service.IKubernetesClientCSRService;
import com.ruoyi.kubernetes.utils.KRsaUtils;
import io.fabric8.kubernetes.api.model.certificates.v1.CertificateSigningRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;

@Slf4j
@Service
public class KubernetesCSRService implements IKubernetesClientCSRService {

    @Autowired
    private KubernetesClientService kubernetesClientService;

    private static int DAYILY_SECONDS = 86400;

    @Override
    public CSRResponseData createCSR(CSRRequestData csrRequestData) {
        CSRResponseData responseData = new CSRResponseData();
        CertificateSigningRequest csrTemplate = getCSRTemplate();
        csrTemplate.getMetadata().setName(csrRequestData.getUsername());
        ClientCSRData clientCSRData = KRsaUtils.generateRSAKeyPair(csrRequestData.getUsername(),csrRequestData.getGroup());
        responseData.setClient_privateKey(clientCSRData.getClient_privateKey());
        responseData.setClient_publicKey(clientCSRData.getClient_publicKey());
        csrTemplate.getSpec().setRequest(clientCSRData.getClient_csr());
        csrTemplate.getSpec().setExpirationSeconds(csrRequestData.getExpirationDays()*DAYILY_SECONDS);
        try {
            kubernetesClientService
                    .getKubernetesClient()
                    .certificates()
                    .v1()
                    .certificateSigningRequests()
                    .create(csrTemplate);
            responseData.setSuccess(true);
        } catch (Exception exception) {
            log.error(exception.getMessage());
            responseData.setSuccess(false);
        }
        return responseData;
    }

    @Override
    public String approveCSR(String csrName) {
        try {
            CertificateSigningRequest csr = kubernetesClientService
                    .getKubernetesClient()
                    .certificates()
                    .v1()
                    .certificateSigningRequests()
                    .withName(csrName)
                    .approve();
            return csr.getSpec().getRequest();
        } catch (Exception exception) {
            log.error(exception.getMessage());
            return null;
        }
    }

    @Override
    public boolean denyCSR(String csrName) {
        try {
            kubernetesClientService
                    .getKubernetesClient()
                    .certificates()
                    .v1()
                    .certificateSigningRequests()
                    .withName(csrName)
                    .deny();
            return true;
        } catch (Exception exception) {
            log.error(exception.getMessage());
            return false;
        }
    }

    @Override
    public String getCRTString(String csrName) {
        return kubernetesClientService
                .getKubernetesClient()
                .certificates()
                .v1()
                .certificateSigningRequests()
                .withName(csrName)
                .get()
                .getStatus()
                .getCertificate();
    }

    private CertificateSigningRequest getCSRTemplate() {
        try {
            File file = ResourceUtils.getFile("classpath:kubernetes-templates/csr-template.yml");
            return kubernetesClientService
                    .getKubernetesClient()
                    .certificates()
                    .v1()
                    .certificateSigningRequests()
                    .load(file)
                    .get();
        }catch (Exception exception){
            log.error(exception.getMessage());
            return null;
        }
    }
}
